﻿using System;
using System.Threading;
using System.Windows.Forms;
using LDSModel.Dal;

namespace LDSProject.Controls
{
    public partial class AreaControl : UserControl
    {
        int _width;
        int _height;
        int _cWidth;
        int _cHeight;

        public AreaControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        public void Initialization()
        {
            Controls.Clear();

            var index = 0;

            _width = Width;
            _height = Height;

            foreach (var o in DeviceService.Instance.OilTankList)
            {
                var control = new OilTankControl(o.OId);
                var blWidth = ((Width - 40) / 2.0f) / control.Width;
                var blHeight = ((Height - 40) / 2.0f) / control.Height;
                var bl = Math.Min(blWidth, blHeight);
                control.Width = (int)(control.Width * bl);
                control.Height = (int)(control.Height * bl);
                control.Left = ((Width - 40) / 2) * (index % 2) + (((Width - 40) / 2 - control.Width) / 2) + 10;
                control.Top = (control.Height + 10) * (index / 2) + 10;
                Controls.Add(control);

                index++;

                _cWidth = control.Width;
                _cHeight = control.Height;

                Thread.Sleep(200);
            }
        }

        private void AreaControl_Resize(object sender, EventArgs e)
        {
            var blWidth = (float)Width / _width;
            var blHeight = (float)Height / _height;
            var bl = Math.Min(blWidth, blHeight);

            var index = 0;

            foreach (Control control in Controls)
            {
                control.Width = (int)(_cWidth * bl);
                control.Height = (int)(_cHeight * bl);
                control.Left = ((Width - 40) / 2) * (index % 2) + (((Width - 40) / 2 - control.Width) / 2) + 10;
                control.Top = (control.Height + 10) * (index / 2) + 10;

                index++;
            }
        }
    }
}
